<?php
add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type('cat_head', array(	'label' => 'Custom category headers','description' => 'Custom category header','public' => false,'show_ui' => true,'show_in_menu' => true, 'menu_position' => 57, 'menu_icon' => get_stylesheet_directory_uri() . '/images/category_header.png','exclude_from_search' => true, 'capability_type' => 'post','hierarchical' => false,'rewrite' => false,'query_var' => true,'supports' => array('title', 'thumbnail'),'taxonomies' => array('category',),'labels' => array (
  'name' => 'Category style',
  'singular_name' => 'Category styles',
  'menu_name' => 'Category style',
  'add_new' => 'Add Category style',
  'add_new_item' => 'Add New Category style',
  'edit' => 'Edit',
  'edit_item' => 'Edit Category style',
  'new_item' => 'New Category style',
  'view' => 'View Category style',
  'view_item' => 'View Category style',
  'search_items' => 'Search Category style',
  'not_found' => 'No Category style',
  'not_found_in_trash' => 'No Category style in Trash',
  'parent' => 'Parent Category style',
),) );
}

add_action( 'add_meta_boxes', 'category_opt_meta_box_add' );
function category_opt_meta_box_add() {
	add_meta_box( 'cat_option_choice', 'Category title', 'cat_opt_meta_box', 'cat_head', 'normal', 'high' );
}


function cat_opt_meta_box( $post )
{
	$values = get_post_custom( $post->ID );
	$check_cat = isset( $values['cat_title_check'] ) ? esc_attr( $values['cat_title_check'][0] ) : '';
	wp_nonce_field( 'cat_opt_meta_box_nonce', 'meta_box_nonce_cat' );
	?>
	
	<input type="checkbox" name="cat_title_check" id="cat_title_check" <?php checked( $check_cat, 'on' ); ?> />
	<label for="cat_title_check"><span><strong>Do not show category title </strong></span></label>
	<br/><br/><span style="font-size:11px; color:#999; line-height:1.3;">* This option will disable title only for chosen category. You can disable and customize all category titles at once in "Theme Options"</span>
	<p style="font-weight:bold; line-height:1.5;"><span style="color:red;">IMPORTANT: </span><br/>
Please remember to choose <span style="color:red;">ONLY ONE</span> category for this customization.  <br/>
All filled or selected fields will be applied to your chosen category.
</p>
	

	<?php	
}


add_action( 'save_post', 'cat_meta_box_save' );
function cat_meta_box_save( $post_id )
{
	// Bail if we're doing an auto save
	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	
	// if our nonce isn't there, or we can't verify it, bail
	if( !isset( $_POST['meta_box_nonce_cat'] ) || !wp_verify_nonce( $_POST['meta_box_nonce_cat'], 'cat_opt_meta_box_nonce' ) ) return;
	
	// if our current user can't edit this post, bail
	if( !current_user_can( 'edit_post' ) ) return;
	
	// now we can actually save the data
	$allowed = array( 
		'a' => array( // on allow a tags
		'href' => array() // and those anchords can only have href attribute
		)
	);
	
	$chk = ( isset( $_POST['cat_title_check'] ) && $_POST['cat_title_check'] ) ? 'on' : 'off';
	update_post_meta( $post_id, 'cat_title_check', $chk );
		
}

//**************************************************************************************************//

add_action( 'add_meta_boxes', 'category_extra_meta_box_add' );
function category_extra_meta_box_add() {
	add_meta_box( 'extra_option_choice', 'Content element options', 'extra_opt_meta_box', 'cat_head', 'normal', 'high' );
}


function extra_opt_meta_box( $post )
{
	$values = get_post_custom( $post->ID );
	$logo = isset( $values['cat_logo'] ) ? esc_attr( $values['cat_logo'][0] ) : '';
	$title_bg = isset( $values['title_bg'] ) ? esc_attr( $values['title_bg'][0] ) : '';
	$line_bg = isset( $values['line_bg'] ) ? esc_attr( $values['line_bg'][0] ) : '';
	$title_txt_color = isset( $values['title_txt_color'] ) ? esc_attr( $values['title_txt_color'][0] ) : '';
	$cat_title_bg = isset( $values['cat_title_bg'] ) ? esc_attr( $values['cat_title_bg'][0] ) : '';
	$cat_title_txt = isset( $values['cat_title_txt'] ) ? esc_attr( $values['cat_title_txt'][0] ) : '';
	$menu_link_color = isset( $values['menu_link_color'] ) ? esc_attr( $values['menu_link_color'][0] ) : '';
	$menu_link_hover = isset( $values['menu_link_hover'] ) ? esc_attr( $values['menu_link_hover'][0] ) : '';


	wp_nonce_field( 'extra_opt_meta_box_nonce', 'meta_box_nonce_extra' );
	?>
	<table style="width:100%;" cellpadding="5" cellspacing="0">
	<tr>
	<td><span><strong>Title background color:</strong></span><br/>
	<input style="width:85px;" type="text" name="title_bg" id="title_bg" value="<?php echo $title_bg; ?>" />		
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span>
	</td>
	<td>	<span><strong>Title bottom line color:</strong></span><br/>
	<input style="width:85px;" type="text" name="line_bg" id="line_bg" value="<?php echo $line_bg; ?>" />
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>		
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span>
	</td>
	<td><span><strong>Title text color:</strong></span><br/>
	<input style="width:85px;" type="text" name="title_txt_color" id="title_txt_color" value="<?php echo $title_txt_color; ?>" />
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>	
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span></td>
	</tr><tr><td></td></tr><tr>
	
	<td><span><strong>Category title background:</strong></span><br/>
	<input style="width:85px;" type="text" name="cat_title_bg" id="cat_title_bg" value="<?php echo $cat_title_bg; ?>" />
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>		
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span></td>
	<td><span><strong>Category title text color:</strong></span><br/>
	<input style="width:85px;" type="text" name="cat_title_txt" id="cat_title_txt" value="<?php echo $cat_title_txt; ?>" />
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>		
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span></td>
	</tr><tr><td></td></tr><tr>
	
	<td><span><strong>Menu link color:</strong></span><br/>
	<input style="width:85px;" type="text" name="menu_link_color" id="menu_link_color" value="<?php echo $menu_link_color; ?>" />
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>		
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span></td>
	<td><span><strong>Menu link hover color:</strong></span><br/>
	<input style="width:85px;" type="text" name="menu_link_hover" id="menu_link_hover" value="<?php echo $menu_link_hover; ?>" />
		<a href="javascript: void(0)" onclick="window.open('http://www.colorpicker.com/', 'windowname1', 'width=582, height=496, left=50px'); return false;" title="Color picker"><img src="<?php echo su_plugin_url(); ?>/images/generator/colorpicker.png" alt="" /></a>
	<br/>		
	<span style="font-size:11px; color:#999; line-height:1.3;">* Example: #FFCC66</span></td>
	</tr>
	</table>
	<br/>
	<p><span><strong>Different color LOGO url:</strong></span><br />
	<input style="border:1px solid #27b5ea; width:100%;" type="text" name="cat_logo" id="cat_logo" value="<?php echo $logo; ?>" /><br/>
	<span style="font-size:11px; color:#999; line-height:1.3;">* Make sure you input FULL urls to the image (including http://) </span></p>

	<?php	
}


add_action( 'save_post', 'extra_meta_box_save' );
function extra_meta_box_save( $post_id )
{
	// Bail if we're doing an auto save
	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	
	// if our nonce isn't there, or we can't verify it, bail
	if( !isset( $_POST['meta_box_nonce_extra'] ) || !wp_verify_nonce( $_POST['meta_box_nonce_extra'], 'extra_opt_meta_box_nonce' ) ) return;
	
	// if our current user can't edit this post, bail
	if( !current_user_can( 'edit_post' ) ) return;
	
	// now we can actually save the data
	$allowed = array( 
		'a' => array( // on allow a tags
		'href' => array() // and those anchords can only have href attribute
		)
	);
	
	if( isset( $_POST['cat_logo'] ) )
		update_post_meta( $post_id, 'cat_logo', wp_kses( $_POST['cat_logo'], $allowed ) );
		
	if( isset( $_POST['title_bg'] ) )
		update_post_meta( $post_id, 'title_bg', wp_kses( $_POST['title_bg'], $allowed ) );
		
	if( isset( $_POST['line_bg'] ) )
		update_post_meta( $post_id, 'line_bg', wp_kses( $_POST['line_bg'], $allowed ) );
		
	if( isset( $_POST['title_txt_color'] ) )
		update_post_meta( $post_id, 'title_txt_color', wp_kses( $_POST['title_txt_color'], $allowed ) );
		
	if( isset( $_POST['cat_title_bg'] ) )
		update_post_meta( $post_id, 'cat_title_bg', wp_kses( $_POST['cat_title_bg'], $allowed ) );
		
	if( isset( $_POST['cat_title_txt'] ) )
		update_post_meta( $post_id, 'cat_title_txt', wp_kses( $_POST['cat_title_txt'], $allowed ) );
		
	if( isset( $_POST['menu_link_color'] ) )
		update_post_meta( $post_id, 'menu_link_color', wp_kses( $_POST['menu_link_color'], $allowed ) );
		
	if( isset( $_POST['menu_link_hover'] ) )
		update_post_meta( $post_id, 'menu_link_hover', wp_kses( $_POST['menu_link_hover'], $allowed ) );
		
}

?>